﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XQ.RequestOfficeSupplies.DAL.DAL
{
    public class DictCategoryDal
    {
        /// <summary>
        /// 获取字典类型列表
        /// </summary>
        /// <returns></returns>
        public List<Sys_DicCategory> GetDictCategoryList()
        {
            List<Sys_DicCategory> list = new List<Sys_DicCategory>();
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                list = ce.Sys_DicCategory.Where(dc=> dc.IsDel != true || dc.IsDel == null).ToList();
            }

            return list;
        }

        public Sys_DicCategory GetDictCategoryById(int dictCategoryId)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                return ce.Sys_DicCategory.First(dc => dc.ID == dictCategoryId);
            }
        }

        /// <summary>
        /// 新增字典项
        /// </summary>
        /// <param name="dicCategory"></param>
        /// <returns></returns>
        public bool SaveDictCategory(Sys_DicCategory dicCategory)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                ce.Sys_DicCategory.Add(dicCategory);
                if (ce.SaveChanges() == 1)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 更新字典项
        /// </summary>
        /// <param name="dicCategory"></param>
        /// <returns></returns>
        public bool UpdateDictCategory(Sys_DicCategory dicCategory)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                Sys_DicCategory oldDc = ce.Sys_DicCategory.First(dc => dc.ID == dicCategory.ID);
                oldDc.Title = dicCategory.Title;
                oldDc.Code = dicCategory.Code;
                oldDc.Sortnum = dicCategory.Sortnum;
                oldDc.Remark = dicCategory.Remark;
                if (ce.SaveChanges() == 1)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 删除字典类型（标识删除）
        /// </summary>
        /// <param name="dictCategoryId"></param>
        /// <returns></returns>
        public bool DeleteDictCategory(int dictCategoryId)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                //标识删除
                Sys_DicCategory oldDicCategory = ce.Sys_DicCategory.First(dc => dc.ID == dictCategoryId);
                oldDicCategory.IsDel = true;
                if (ce.SaveChanges() == 1)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 批量删除字典类型
        /// </summary>
        /// <returns></returns>
        public bool DelDictCategorys(List<int> dictCategoryIds)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                if (dictCategoryIds != null && dictCategoryIds.Count > 0)
                {
                    foreach (int dictCategoryId in dictCategoryIds)
                    {
                        //标识删除
                        Sys_DicCategory oldDicCategory = ce.Sys_DicCategory.FirstOrDefault(dc => dc.ID == dictCategoryId);
                        oldDicCategory.IsDel = true;
                    }
                    return ce.SaveChanges() > 0;
                }
            }
            return false;
        }

        /// <summary>
        /// 检查是否有同名的
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public bool IsHasSameName(string categoryName)
        {
            return IsHasSameName(categoryName, 0);
        }

        /// <summary>
        /// 检查是否有同名的
        /// </summary>
        /// <param name="categoryName"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public bool IsHasSameName(string categoryName, int categoryId)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                int n = ce.Sys_DicCategory.Count(dc => dc.Title == categoryName && dc.ID != categoryId);
                if (n > 0)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
